#!/bin/csh
# make_timeseries_point.csh
# PRELIMINARY VERSION 
######################################################################################
# Create the timeseries of all variables stored in grd files at a given grid point
# The output file is named timeseries_point.dat and is stored in the parent simulation directory
#
# Run it using ./make_timeseries_point.csh nline
# where nline is the line of a grd_***_.dat file of the grid point of interest
######################################################################################

source simulation.dir

if (! -d $dirout) then
  echo $dirout 'does not exist. Check the path stored in the variable "dirout" '
  exit
endif

set nline = ${1}
if (${1} == "") then
  echo 'Line number of grd_***_.dat file of the grid point is missing'
  exit
endif

#------------------------------------------------------
# Make a loop over all grd files found in dirout
#------------------------------------------------------
set outfile = timeseries_point.dat
touch $outfile

set files = (`ls ${dirout}grd*.dat`)

foreach file ($files)
     echo "Current file:" $file

# Extract the current time from the simulation file
     set ctime = `awk 'NR == 1 {print $5}' $file`

# Write into tmp1 the current simulation time
     awk 'BEGIN {printf "%s", "'"$ctime"'" }' >! tmp1
   
# Write into tmp2 all corresponding fields contained in the grd file
     awk 'NR=='"$nline"' ' $file >! tmp2

# Paste together tmp1 and tmp2
     paste tmp1 tmp2 >! tmp3

# Append tmp3 to the final file
     cat tmp3 >> $outfile

     rm -f tmp*

  end

# Move outfile into the parent simulation directory
  mv $outfile $dirout 

endif

